package auth

import (
	"crypto/sha256"
	"fmt"
	"goProject/cfg"
	"goProject/table"
)

// VerifyPassword 校验密码是否正确
func VerifyPassword(user table.User, pwd string) bool {
	if user.Password == CypherPassword(user.Username, pwd) {
		return true
	}
	return false
}

// CypherPassword 把明文密码，加密成密文密码
func CypherPassword(username string, pwd string) string {
	if cfg.Password.PwdSecretKey == "" {
		return pwd
	}
	hash := fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(username, "_", cfg.Password.PwdSecretKey, "_", pwd))))
	return hash
}
